package com.dats.db;

import com.dats.entity.BaseObj;
import com.dats.entity.CacheBean;
import com.dats.hot.DManager;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author sig
 * @since 2016/9/25
 */
public class NameMap {
    private static Map<Class<?>, NameKeyUnit> tableMap = new ConcurrentHashMap<>();

    public static void put(Class<?> clazz, String tableName) {
        NameKeyUnit unit = new NameKeyUnit(tableName);
        tableMap.put(clazz, unit);

        if (!BaseObj.class.isAssignableFrom(clazz)) return;
        Class<BaseObj> cls = (Class<BaseObj>) clazz;
        DManager.dataMg().loadBase(cls);
    }

    public static void put(Class clazz, NameKeyUnit unit) {
        tableMap.put(clazz, unit);

        if (!unit.isBase()) {
            Class<CacheBean> cls = (Class<CacheBean>) clazz;
            DManager.idMg().putMaxId(cls);
        }
    }

    public static String getTableName(Class<?> clazz) {
        NameKeyUnit unit = getNameKeyUnit(clazz);
        return null == unit ? null : unit.getTableName();
    }

    public static NameKeyUnit getNameKeyUnit(Class<?> clazz) {
        return tableMap.get(clazz);
    }
}
